package com.atguigu.ssyx.sys.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.enums.WareType;
import com.atguigu.ssyx.model.sys.Ware;
import com.atguigu.ssyx.sys.service.WareService;
import com.atguigu.ssyx.vo.product.WareQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;



/**
 * <p>
 * 仓库表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-09-12
 */
@RestController
@Api(tags = "仓库管理接口")
@RequestMapping("/admin/sys/ware")

public class WareController {

    @Autowired
    private WareService wareService;

    //获取仓库列表(带搜索)
//    url: `${api_name}/${page}/${limit}`,
//    method: 'get',
    //params: searchObj
    @ApiOperation("获取仓库列表(带搜索)")
    @GetMapping("/{page}/{limit}")
    public Result getWarePage(@PathVariable Long page,
                              @PathVariable Long limit,
                              WareQueryVo queryVo) {
        Page<Ware> pageparam = new Page<>(page, limit);
        IPage<Ware> pageModel = wareService.selectPageWare(pageparam,queryVo);
        return Result.ok(pageModel);
    }

    //根据id查询仓库
    // url: `${api_name}/get/${id}`,
    //      method: 'get'
    @ApiOperation("根据id查询仓库")
    @GetMapping("/get/{id}")
    public Result selectWareById(@PathVariable Long id) {
        Ware wareById = wareService.getById(id);
        if (wareById != null) {
            return Result.ok(wareById);
        }
        return Result.fail(WareType.NotFind);
    }

    //添加仓库
    //   url: `${api_name}/save`,
    //      method: 'post',
    //      data: role
    @ApiOperation("添加仓库")
    @PostMapping("/save")
    public Result saveWare(@RequestBody Ware ware) {
        Ware selectWareByName = wareService.selectWareByName(ware.getName());
        if (selectWareByName != null) {
          if(selectWareByName.getIsDeleted()==0){
              return Result.fail(WareType.IsExist);
          }else{
              wareService.updateIsdeleteById(selectWareByName.getId());
              return Result.ok(WareType.IsdeleteReSet);
          }

        }else{
            wareService.save(ware);
            return Result.ok(WareType.Success);
        }

    }

    //更新仓库
//    url: `${api_name}/update`,
//    method: 'put',
//    data: role
    @ApiOperation("更新仓库")
    @PutMapping("/update")
    public Result updateWare(@RequestBody Ware ware) {
        boolean is_success = wareService.updateById(ware);
        if (is_success) {
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    //删除仓库
//    url: `${api_name}/remove/${id}`,
//    method: 'delete'
    @ApiOperation("删除仓库")
    @DeleteMapping("/remove/{id}")
    public Result removeWare(@PathVariable Long id) {
        boolean is_success=wareService.removeById(id);
        if (is_success) {
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    //批量删除
//    url: `${api_name}/batchRemove`,
//    method: 'delete',
//    data: idList
    @ApiOperation("批量删除")
    @DeleteMapping("/delete")
    public Result deleteWare(@RequestBody List<Long> wareIds) {
        boolean is_success = wareService.removeByIds(wareIds);
        if (is_success) {
            return Result.ok(null);
        }else{
            return Result.fail(null);
        }
    }
//查询所有仓库
//    url: `${api_name}/findAllList`,
//    method: 'get'
    @ApiOperation("查询所有仓库")
    @GetMapping("/findAllList")
    public Result selectAllWare() {
        List<Ware> list = wareService.list();
            return Result.ok(list);
    }

}

